import {getDetailInfo} from 'api/api';
export default{
    state:{
       detailInfo:{}
    },
    mutations:{
        getDetailInfo(state,detailInfo){
            state.detailInfo=detailInfo
        },
        switchChecked(state,{attrId,valId}){
            state.detailInfo.spuSaleAttrList.forEach(item=>{
                if(item.id===attrId){
                    item.spuSaleAttrValueList.forEach(val=>{
                        val.isChecked='0';
                        if(val.id===valId){
                            val.isChecked='1'
                        }
                    })
                }
            })
        }
    },
    actions:{
      async getDetailInfo(store,skuId){
        try{
            const {code,data}= await getDetailInfo(skuId)
         //    console.log(data);
             if(code===200){
                 store.commit('getDetailInfo',data)
             }
            //  console.log(data);
            return {code,data}
         }catch(e){
             return Promise.reject(e)
         }
     }
    },  
    getters:{
        categoryView(state){
            return state.detailInfo.categoryView ? state.detailInfo.categoryView:{}
        },
        skuInfo(state){
            return state.detailInfo.skuInfo?state.detailInfo.skuInfo:{}
        },
        spuSaleAttrList(state){
            return state.detailInfo.spuSaleAttrList?state.detailInfo.spuSaleAttrList:[]
        },
        checkedBtns(state,getters){
            let arr=[];
            getters.spuSaleAttrList.forEach(item=>{
                item.spuSaleAttrValueList.forEach(val=>{
                    if(val.isChecked==='1'){
                        arr.push({
                            ...val,
                            attrId:item.id
                        })
                    }
                })
            })
            return arr
        }
        
    }
}